//
//  LGPatientDetailReportCell.m
//  BreathTrainingTool
//
//  Created by Bovin on 2024/7/21.
//  患者自传报告

#import "LGPatientDetailReportCell.h"
#import "LGCheckPhotoTool.h"
#define itemW  viewPix(66)

@interface LGPatientDetailReportCell()
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UILabel *timeLabel;
/**   */
@property (nonatomic , strong)UIView *photoView;
/**   */
@property (nonatomic , strong)NSDictionary *typeDic;
/**   */
@property (nonatomic , strong)NSMutableArray *photoArray;
/**   */
@property (nonatomic , strong)NSArray *picArray;

@end

@implementation LGPatientDetailReportCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.typeDic = @{@"1":@"CT影像检查",@"2":@"检查报告",@"3":@"检验报告",@"4":@"病历单",@"5":@"药物处方单"};
        [self createSubView];
    }
    return  self;
}

-(void)setDataDic:(NSDictionary *)dataDic{
    [super setDataDic:dataDic];
    self.titleLabel.text = self.typeDic[LGNSString(dataDic[@"type"])];
    self.timeLabel.text = LGNSString(dataDic[@"markDate"]);
    NSArray *photoArray = [LGNSString(dataDic[@"picUrl"]) componentsSeparatedByString:@","];
    self.picArray = photoArray;
    for (UIImageView *imageView in self.photoArray) {
        [imageView removeFromSuperview];
    }
    [self.photoArray removeAllObjects];
    CGFloat left = 0;
    CGFloat top = 0;
    CGFloat maxW = (Screen_W-viewPix(88));
    for (NSInteger i=0; i<photoArray.count; i++) {
        top = (left+itemW>maxW)?top+itemW+viewPix(8):top;
        left = (left+itemW>maxW)?0:left;
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(left, top, itemW, itemW)];
        imageView.backgroundColor = [UIColor colorWithString:@"#F6F5FA"];
        [self.photoView addSubview:imageView];
        [imageView sd_setImageWithURL:[NSURL URLWithString:photoArray[i]]];
        left += itemW+viewPix(8);
        [self.photoArray addObject:imageView];
    }
    CGFloat photoH = (photoArray.count>0)?top+itemW:0;
    [self.photoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(photoH));
    }];
}

-(void)setType:(LGPositionType)type{
    [super setType:type];
    CGFloat bottom = (type == LGPositionTypeBottom)?-viewPix(16):-viewPix(8);
    [self.photoView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.contentView).offset(bottom);
    }];
}
-(void)checkPhotoAction{
    LGCheckPhotoTool *photoView = [[LGCheckPhotoTool alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
    photoView.picArray = self.picArray;
    [[ObjectTool mainWindow] addSubview:photoView];
    [photoView showViewAnimation:CGPointMake(0, 0) index:0];
}
#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.timeLabel];
    [self.contentView addSubview:self.photoView];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(viewPix(6));
        make.left.equalTo(self.contentView).offset(viewPix(32));
        make.right.lessThanOrEqualTo(self.timeLabel.mas_left).offset(-viewPix(5));
    }];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset(-viewPix(32));
        make.centerY.equalTo(self.titleLabel);
    }];
    [self.photoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(viewPix(4));
        make.left.equalTo(self.contentView).offset(viewPix(32));
        make.right.equalTo(self.contentView).offset(-viewPix(32));
        make.height.equalTo(@(itemW));
        make.bottom.equalTo(self.contentView).offset(-viewPix(10));
    }];
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"" colorString:@"#2B2A37" textFont:LGFontWeight(15, UIFontWeightSemibold) textAlignment:NSTextAlignmentLeft lines:0];
    }
    return _titleLabel;
}

-(UILabel *)timeLabel{
    if(!_timeLabel){
        _timeLabel = [UILabel lableWithText:@"" colorString:@"#61616D" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _timeLabel;
}
-(UIView *)photoView{
    if(!_photoView){
        _photoView = [[UIView alloc]init];
        [_photoView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(checkPhotoAction)]];
    }
    return _photoView;
}

@end
